/*
Copyright 2010 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.screenworker.compactmvc.extensions.air.fileservice
{
	import com.screenworker.compactmvc.extensions.air.interfaces.IFileService;
	import com.screenworker.compactmvc.interfaces.IModel;
	import com.screenworker.compactmvc.patterns.Controller;
	import com.screenworker.compactmvc.patterns.Model;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;

	public class FileService extends Model implements IModel, IFileService
	{
		public static var NAME:String = 'FileService';
		
		private var _fileStream:FileStream;
		
		public function FileService(name:String, controller:Controller)
		{
			super(name, controller);
			_fileStream = new FileStream();
		}
		
		override public function removeHandler():void
		{
			_fileStream.close();
			_fileStream = null;
		}
		
		override public function getName():String
		{
			return NAME;
		}
		
		/**
		 * READ OBJECT FROM FILE
		 * @param filePath
		 * @return 
		 * 
		 */		
		public final function readObject(file:File):* 
		{
			//read the file
			if (!file.exists)
			{
				errorHandler( new Error('error_fileAccessModel_fileNotFound:' + file.nativePath.toString() ) );
				return null;
			}	
			_fileStream.open(file, FileMode.READ);

			try
			{	
				var obj:* = _fileStream.readObject();	
				
			}
			catch (e:Error)
			{
				e.message += ' (FileService->readObject)';
				errorHandler(e);
				_fileStream.close();
				return null;
			}

			_fileStream.close();
			return obj;
		}
		
		/**
		 * WRITE OBJECT TO FILE 
		 * @param obj
		 * @param filePath
		 * 
		 */		
		public final function writeObject(obj:*, file:File, deleteIfExist:Boolean=true):void 
		{	
			//create a file
			if(deleteIfExist)
				deleteObject(file);
				
			try
			{
				_fileStream.open(file, FileMode.WRITE);// and open the file for write
				_fileStream.writeObject(obj);//write the object to the file
				_fileStream.close();
				
			}
			catch (e:Error)
			{
				e.message += ' (FileService->writeObject)';
				errorHandler(e);
			}	
		}	
		
		public final function deleteObject(file:File):Boolean
		{
			if (!file.exists)
				return false;				
			try
			{
				file.deleteFile();
			}
			catch(e:Error)
			{
				errorHandler(e);
				return false;
			}
			return true;
		}
		
		public function errorHandler(e:Error):void 
		{
			//overriden by subclass
        }
		
	}
}